package com.hy.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.bean.Card;
import com.hy.bean.PageBean;
import com.hy.bean.RespBean;
import com.hy.bean.RespPageBear;
import com.hy.bean.UmsUser;
import com.hy.dao.CardDao;
import com.hy.service.impl.CardPageServiceImpl;
import com.hy.vo.CardUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Source;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @Description: 帖子管理
 * @Author: zhangzhengwei2006
 * @Date: 2024-01-05 11:09
 *
 */
public class CardController {
    private CardDao cardDao = new CardDao();
    private ObjectMapper objectMapper = new ObjectMapper();

    public PageBean<List<CardUser>> selectAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<CardUser> questionList = this.cardDao.selectPage(1,5);
        PageBean<List<CardUser>> pb = new PageBean();
        if (questionList != null) {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(questionList);
            return pb;
        } else {
            return null;
        }

    }


    public  PageBean<List<Map<String,Object>>> selectFollow(HttpServletRequest request,Integer limit) throws Exception {
        System.out.println("selectFollow==============>");

        HttpSession session  = request.getSession(true);
        Integer id = (Integer) session.getAttribute("id");

        List<Map<String, Object>> mapList = cardDao.selectFollow(id,limit);
        PageBean<List<Map<String, Object>>>  pb  = new PageBean<>();

        if(mapList != null){
            pb.setCode(1);
            pb.setMsg("根据关注查询");
            pb.setStatus(500);
            pb.setData(mapList);
            return pb;
        } else {
            return null;
        }

        //return null;
    }
//PageBean<Map<String,Object>>
    public PageBean<List<Map<String, Object>>> selectRecommend(HttpServletRequest request,Integer limit) throws Exception {
        System.out.println("selectRecommend===========>");
//        String idstr = request.getParameter("id");
//        String limitstr = request.getParameter("limit");

//        if(idstr == null||limitstr == null){return  null;}

//        int id = Integer.parseInt(idstr);
//        int limit  = Integer.parseInt(limitstr);
        HttpSession session = request.getSession(true);
        Integer id = (Integer) session.getAttribute("id");
        List<Map<String, Object>> mapList = cardDao.selectRecommend(id,limit);
        PageBean<List<Map<String, Object>>>  pb  = new PageBean<>();

        if(mapList != null){
            pb.setCode(1);
            pb.setMsg("根据推荐查询");
            pb.setStatus(500);
            pb.setData(mapList);
            return pb;
        } else {
            return null;
        }

//        return null;
    }
    public RespPageBear getList(Integer currentPage, Integer pageSize) throws Exception {
        return new CardPageServiceImpl().list(currentPage,pageSize);
    }

    public RespBean updateStatus(Integer id, String status) throws Exception {
        System.out.printf("id=%d,status=%s",id,status);
        return new CardDao().updateStatusByid(id, status);
    }



    public PageBean<List<Map<String, Object>>> listAllCard() throws Exception {
        List<Map<String, Object>> mapList = cardDao.selectAllx();
        PageBean<List<Map<String, Object>>> pb = new PageBean<>();
        pb.setMsg("加载数据成功！");
        pb.setStatus(500);
        pb.setData(mapList);
        pb.setCode(000100);
        return pb;
    }

    public PageBean<List<Map<String, Object>>> selectAllASAP() throws Exception {
        List<Map<String, Object>> mapList = cardDao.selectAllASAP();
        PageBean<List<Map<String, Object>>> pb = new PageBean<>();
        pb.setMsg("加载数据成功！");
        pb.setStatus(500);
        pb.setData(mapList);
        pb.setCode(000100);
        return pb;
    }

     public PageBean modifyCard(int cid) throws Exception {
         boolean falg = cardDao.modify(cid);
         PageBean pb = new PageBean();

         pb.setMsg("修改成功");
         pb.setCode(01010);
         pb.setStatus(500);
         pb.setData(falg);
         return pb;
     }

    public PageBean modifyCard2(int cid) throws Exception {
        boolean falg2= cardDao.modify2(cid);
        PageBean pb = new PageBean();

        pb.setMsg("修改成功");
        pb.setCode(010100);
        pb.setStatus(500);
        pb.setData(falg2);
        return pb;
    }


    public PageBean modifyFavor(int cid) throws Exception {
        boolean flag= cardDao.modifyfa(cid);
        PageBean pb = new PageBean();

        pb.setMsg("修改成功");
        pb.setCode(010100);
        pb.setStatus(500);
        pb.setData(flag);
        return pb;
    }

    public PageBean modifyFavor2(int cid) throws Exception {
        boolean flag2= cardDao.modifyfa2(cid);
        PageBean pb = new PageBean();

        pb.setMsg("修改成功");
        pb.setCode(010100);
        pb.setStatus(500);
        pb.setData(flag2);
        return pb;
    }


    public PageBean<List<Card>> selectSy(String str) throws Exception {
        List<Card> cardList = cardDao.selectSy(str);

        PageBean<List<Card>> pb = new PageBean<>();
        System.out.println(pb+"======");
        pb.setMsg("返回查询数据");
        pb.setStatus(500);
        pb.setData(cardList);
        pb.setCode(0101010);
        return pb;
    }
}
